/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       blended_bitmap_window.cpp

	$Header: /game/blended_bitmap_window.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "blended_bitmap_window.h"

#include "bitmap_layer.h"
#include "paint_surface.h"

// --------------------------------------------------------------------------
// a bitmap window with optional alpha blending
// --------------------------------------------------------------------------
t_blended_bitmap_window::t_blended_bitmap_window( t_bitmap_layer const* bitmap, 
												  t_screen_point point, t_window* parent, 
												  bool use_offset, int alpha )
				       : t_bitmap_layer_window( bitmap, point, parent, use_offset )
{
	m_alpha = -1;
	set_alpha( alpha );
};

// --------------------------------------------------------------------------
// a bitmap window with optional alpha blending
// --------------------------------------------------------------------------
void t_blended_bitmap_window::paint( t_paint_surface& paint_surface )
{
	if (m_alpha >= 15)
	{
		t_bitmap_layer_window::paint( paint_surface );
		return;
	}
	if (m_alpha == 0)
		return;

	t_abstract_bitmap16_ptr	dest = paint_surface.get_bitmap();
	t_screen_point          dest_point = -paint_surface.get_rect().top_left();

	m_bitmap->draw_to( m_bitmap->get_rect(), *dest, dest_point, m_alpha );
}

// --------------------------------------------------------------------------
// a bitmap window with optional alpha blending
// --------------------------------------------------------------------------
void t_blended_bitmap_window::on_bitmap_changed()
{
	if (m_bitmap == 0)
		m_has_transparency = k_completely_transparent;
	else if (m_alpha == 0)
		m_has_transparency = k_completely_transparent;
	else if (m_alpha < 15)
		m_has_transparency = k_some_transparency;
	else if (m_bitmap->get_alpha_depth() > 0)
		m_has_transparency = k_some_transparency;
	else
		m_has_transparency = k_no_transparency;
}

// --------------------------------------------------------------------------
// a bitmap window with optional alpha blending
// --------------------------------------------------------------------------
void t_blended_bitmap_window::set_alpha( int alpha )
{
	if (alpha < 0)
		alpha = 0;
	if (alpha > 15)
		alpha = 15;
	if (alpha == m_alpha)
		return;

	bool was_visible = is_visible();

	set_visible( false );
	m_alpha = alpha;
	on_bitmap_changed();
	set_visible( was_visible );
}
